package com.tjnu.losca.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjnu.losca.Vo.ResponseVO;
import com.tjnu.losca.pojo.Goods;
import com.tjnu.losca.service.IGoodsService;
import com.tjnu.losca.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author losca
 * @since 2022-02-13
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    IGoodsService goodsService;
    @Autowired
    FileUtils fileUtils;
    @GetMapping("/getGoods")
    public ResponseVO<IPage> getUsers(@RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "8") int pageSize,
                                      @RequestParam(defaultValue = "") String search)  {
        return ResponseVO.success(goodsService.list(pageNum, pageSize, search));
    }
    @CacheEvict(value = "category",key = "'category'")
    @PostMapping("/del")
    public ResponseVO<Boolean> del(String goodId){
        return ResponseVO.success(goodsService.del(goodId));
    }
    @CacheEvict(value = "category",key = "'category'")
    @PostMapping("/rec")
    public ResponseVO<Boolean> rec(String goodId){
        System.out.println(goodId);
        return ResponseVO.success(goodsService.rec(goodId));
    }
    @CacheEvict(value = "category",key = "'category'")
    @PostMapping("/mulDel")
    public ResponseVO<Boolean> mulDel(String[] ids){
        return ResponseVO.success(goodsService.mulDel(ids));
    }
    @CacheEvict(value = "category",key = "'category'")
    @PostMapping("/update")
    public ResponseVO<Boolean> update(Goods goods){
        return ResponseVO.success(goodsService.update(goods));
    }

    @CacheEvict(value = "category",key = "'category'")
    @PostMapping("/add")
    public ResponseVO<Boolean> add(Goods goods, MultipartFile file) throws Exception {
        String image = "http://192.168.142.202:9000/img/" + fileUtils.imgUpload(file);
        goods.setImageHead(image);
        goods.setImage(image);
        goods.setImageFoot(image);
        goods.setIcon(image);
        return ResponseVO.success(goodsService.save(goods));
    }

}
